import {BasePage, BasePageData} from "../common/core/BasePage";
import { field } from "../../modules/coreModule/data/DataLoader";
import {waitForLogin} from "../../modules/userModule/managers/UserManager";
import {waitForDataLoad} from "../../modules/coreModule/managers/DataManager";
import {page, pageFunc} from "../common/PageBuilder";
import {pageMgr} from "../../modules/coreModule/managers/PageManager";
import {DiscoverInterface} from "../../modules/discoverModule/discoverCommon";
import {DiscoverPage} from "../discover/DiscoverPage";
import {alertMgr} from "../../modules/coreModule/managers/AlertManager";
import {UploaderPage} from "../common/partPages/UploaderPage";
import {CloudFileUtils, MediaFile} from "../../utils/CloudFileUtils";

class Data extends BasePageData {
  /* @field
  posts = []; */
  @field
  fileList = [];
  @field
  content = "";
  @field
  checked = false;
  @field([Object])
    images: MediaFile[] = [];
}

@page("post-activity", "发布动态")
export class PostActivityPage extends BasePage<Data> {
  data = new Data();

/*   public uploaderPage: UploaderPage = new UploaderPage("discover", "discover"); */
  async onLoad(e){
    super.onLoad(e);
    /* await this.loadPosts(); */
  }

  @pageFunc
  public backToDiscover(){
    pageMgr().switch(DiscoverPage).then();
  }
/*
  @pageFunc
  public afterRead(event){
    const { file } = event.detail;
    console.log(file);
    let fileListCopy = this.data.fileList;
    for(let i=0;i<file.length;i++)
      fileListCopy.push(file[i]);
    this.setData({
      fileList: fileListCopy
    })
    console.log(fileListCopy);
  }
 */
  @pageFunc
  public contentChange(event){
    this.setData({content: event.detail.value})
    console.log(this.data.content)
  }
  @pageFunc
  @waitForLogin
  public async post(){
    console.log(this.data.images);
    /* let fileId = await this.uploadPictures(); */
    this.uploadPictures().then(res => {
      console.log(res)
      let sendData = {
        images: res,
        content: this.data.content,
        isReport: this.data.checked
      }
      console.log(sendData);
          let send = (DiscoverInterface.AddPost({...sendData}))
          alertMgr().showAlert("提交成功，审核通过后即可发布").then(() => {
            this.backToDiscover();
          });
    })
  }
  @pageFunc
  public onCheck(event){
    this.setData({
      checked: event.detail,
    });
  }
  @pageFunc
	public async onPicturesChoose(e: CustomEvent) {
    if (e.detail.file instanceof Array)
      this.data.images.push(...e.detail.file)
    else
      this.data.images.push(e.detail.file)

    await this.setData({images: this.data.images});
	}

	@pageFunc
	public async onPicturesDelete(e: CustomEvent) {
		this.data.images.splice(e.detail.index, 1);
        await this.setData({images: this.data.images});
	}

	public async uploadPictures() {
    console.log(this.data.images)
    return new Promise(resolve => {
      if(this.data.images){
        const file =  this.data.images.map(f => CloudFileUtils.mediaFile2File("discover", f));
        CloudFileUtils.uploadFiles(file, "discover").then(res => {
          console.log(res);
          resolve(res);
        })
      }
    })
	}
/*   private async uploadPictures() {
		const fileIds = await this.uploaderPage.uploadPictures();
		await this.setData({images: fileIds});
	} */
}
